﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DigitalSolutionLibrary.SqlDatabase;
using System.Threading;
using System.IO;
using System.Xml;

namespace DigitalSolutionManagement
{
    public partial class frmDBConfig : BaseFormEdit
    {
        private Thread th;
        private XmlDocument xDoc;
        private string appConfigPath;

        public override BaseFormEdit GetNewInstance(BaseFormEdit.OpenMode mode)
        {
            return new frmDBConfig();
        }

        public frmDBConfig()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private bool CheckInput()
        {
            if (txtServerName.Text == String.Empty)
            {
                txtServerName.Focus();
                MessageBox.Show("Vui lòng nhập tên server", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (txtDBName.Text == String.Empty)
            {
                txtDBName.Focus();
                MessageBox.Show("Vui lòng nhập tên cơ sở dữ liệu", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (txtUserName.Text == String.Empty)
            {
                txtUserName.Focus();
                MessageBox.Show("Vui lòng nhập tên đăng nhập", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (txtPassword.Text == String.Empty)
            {
                txtPassword.Focus();
                MessageBox.Show("Vui lòng nhập mật khẩu", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        private void CheckConnection()
        {
            if (DatabaseWrapper.CheckConnection(txtServerName.Text, txtDBName.Text, txtUserName.Text, txtPassword.Text))
            {
                processing1.HideProcessing();
                MessageBox.Show("Kết nối thành công", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                processing1.HideProcessing();
                MessageBox.Show("Kết nối không thành công", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            xDoc.SelectSingleNode("/AppConfig/ServerName").InnerText = txtServerName.Text;
            xDoc.SelectSingleNode("/AppConfig/DBName").InnerText = txtDBName.Text;
            xDoc.SelectSingleNode("/AppConfig/UserName").InnerText = txtUserName.Text;
            string pass = DigitalSolutionLibrary.Security.TripleDES.Encryption(txtPassword.Text);
            xDoc.SelectSingleNode("/AppConfig/Password").InnerText = pass;
            xDoc.Save(appConfigPath);
            
            Application.Restart();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                processing1.ShowProcessing();
                th = new Thread(() => CheckConnection());
                th.Start();
            }
        }

        private void frmDBConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (th != null && th.IsAlive)
                th.Abort();
        }

        private void frmDBConfig_Load(object sender, EventArgs e)
        {
            appConfigPath = Application.StartupPath + @"\AppConfig.htd";
            if (File.Exists(appConfigPath))
            {
                xDoc = new XmlDocument();
                xDoc.Load(appConfigPath);
            }
            else
            {
                string xmlConfig = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
                xmlConfig += "<AppConfig>";
                xmlConfig += "<ServerName></ServerName>";
                xmlConfig += "<DBName></DBName>";
                xmlConfig += "<UserName></UserName>";
                xmlConfig += "<Password></Password>";
                xmlConfig += "<AuthEmail></AuthEmail>";
                xmlConfig += "<AuthPass></AuthPass>";
                xmlConfig += "</AppConfig>";

                xDoc = new XmlDocument();
                xDoc.LoadXml(xmlConfig);
            }

            txtServerName.Text = xDoc.SelectSingleNode("/AppConfig/ServerName").InnerText;
            txtDBName.Text = xDoc.SelectSingleNode("/AppConfig/DBName").InnerText;
            txtUserName.Text = xDoc.SelectSingleNode("/AppConfig/UserName").InnerText;
            string pass = xDoc.SelectSingleNode("/AppConfig/Password").InnerText;
            txtPassword.Text = DigitalSolutionLibrary.Security.TripleDES.Decryption(pass);
        }
    }
}
